package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.XcUserRepository;
import com.qf.pojo.po.XcUser;
import com.qf.pojo.req.XcUserReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.PersonalService;
import com.qf.utils.UploadUtils;
import com.qf.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    XcUserRepository xcUserRepository;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserUtils userUtils;

    @Autowired
    UploadUtils uploadUtils;


    //查询用户信息
    @Override
    public BaseResp select(HttpServletRequest request) {

        //从request中获得tokend  这个时候拿到的是token字符串
        String token=request.getHeader("token");

        //根据token字符串去查找当初登陆成功的时候存在redis中的json格式的用户--也就是根据key查value---转为实体类
        XcUser xcUser=userUtils.getUserByToken(token);

        if (xcUser==null){

            return new BaseResp(1,"用户未登录",null,null);
        }

        return new BaseResp(0,"查询成功",null,xcUser);
    }

    //修改用户信息
    @Override
    public BaseResp update(XcUserReq xcUserReq, HttpServletRequest request) {

       //从请求域中拿到token串
       String token = request.getHeader("token");

       //根据token串解析出用户对象
       Object xcUser1=redisTemplate.opsForValue().get(token);

       //判断用户是否在redis中存在
       if (xcUser1==null){

           return new BaseResp(1,"用户登录信息已过期",null,null);
       }

       //将解析出来的json用户对象转为实体类型

        String s= JSONObject.toJSONString(xcUser1);
        //这两行是json语法异常--这样转换一下 如果有异常则放开看看
        //Object s1=JSON.parse(s);
        //String s2=s1.toString();
        XcUser xcUser=JSONObject.parseObject(s, XcUser.class);

        //将注册信息写入到解析出来xcUser中（此时信息还没有写入）  source:来源  target：目标
        BeanUtils.copyProperties(xcUserReq,xcUser);

        //将写入的信息保存并且刷新（信息在这一步已经写入了数据库）
        XcUser SqlPojoliveUser=xcUserRepository.saveAndFlush(xcUser);


        if (SqlPojoliveUser ==null) {

            return new BaseResp(1,"修改失败",null,null);
        }

        //将修改后的信息存储到redis中
        redisTemplate.opsForValue().set(token,xcUser);

        return new BaseResp(0,"修改成功",null,null);
    }

    //根据用户老密码修改新密码
    @Override
    public BaseResp updatepasswordbyoldpasswode(HttpServletRequest request, XcUserReq xcUserReq) {

        //先从请求域获得用户状态信息
        String token=request.getHeader("token");

        if (token==null){

            return new BaseResp(1,"登陆状态已经过期",null,null);
        }

        //token存在判断是否有用户信息
        XcUser xcUser=userUtils.getUserByToken(token);

        if (xcUser==null){

            return new BaseResp(1,"登陆状态已经过期",null,null);
        }

        //获得用户数据库的原始密码
        String sqlpassword=xcUser.getPassword();

        //获得用户输入的老密码
        String oldpassword=xcUserReq.getOldpassword();

        if (!sqlpassword.equals(oldpassword)){

            return new BaseResp(1,"原密码输入错误",null,null);
        }

        if (sqlpassword.equals(xcUserReq.getPassword())){

            return new BaseResp(1,"新密码不能与原来密码一样",null,null);
        }

        //将新密码设置到数据库中
        xcUser.setPassword(xcUserReq.getPassword());

        xcUserRepository.saveAndFlush(xcUser);

        //删除token
        //在redis中按token名字查找token
        Boolean delete = redisTemplate.delete(token);

        if (delete){
            return new BaseResp(0,"修改成功",null,null);
        }

        return new BaseResp(1,"修改失败",null,null);

    }

    //上传头像
    @Override
    public BaseResp upload(MultipartFile multipartFilet, HttpServletRequest request) {

        //将图片文件转换成地址值
        String picurl=uploadUtils.upload(multipartFilet);

        //从request中拿到用户信息
        String token=request.getHeader("token");

        //判断是否拿到token
        if(token==null){

            return new BaseResp(1,"用户登录信息已过期",null,null);
        }

        //根据token拿到用户的实体对象
        XcUser xcUser=userUtils.getUserByToken(token);

        if (xcUser==null){

            return new BaseResp(1,"用户登录信息已过期",null,null);
        }

        //将数据库的图片信息更改
        xcUser.setUserPic(picurl);

        xcUserRepository.saveAndFlush(xcUser);

        //将修改后的信息存储到redis中
        redisTemplate.opsForValue().set(token,xcUser);

        return new BaseResp(0,"修改成功",null,null);
    }

    //查找头像
    @Override
    public BaseResp findpic(HttpServletRequest request) {

        //从请求域中拿到token串
        String token = request.getHeader("token");

        //根据token串解析出用户对象
        Object xcUser1=redisTemplate.opsForValue().get(token);

        //判断用户是否在redis中存在
        if (xcUser1==null){

            return new BaseResp(1,"用户登录信息已过期",null,null);
        }

        //将解析出来的json用户对象转为实体类型

        String s= JSONObject.toJSONString(xcUser1);
        //这两行是json语法异常--这样转换一下 如果有异常则放开看看
        //Object s1=JSON.parse(s);
        //String s2=s1.toString();
        XcUser xcUser=JSONObject.parseObject(s, XcUser.class);

        String pic=xcUser.getUserPic();

        if (pic==""||pic==null){

            return new BaseResp(0,"查询失败",null,pic);
        }

        return new BaseResp(0,"查询头像成功",null,pic);
    }

}
